﻿namespace GreenIsomer.Data.Entities.Transform {
	using System;
	using System.Xml;
	using System.Xml.Serialization;
	using System.IO;
	using System.Globalization;

	[Serializable]
	public abstract class Output : InputOutput {

		public override string ToString() {
			return string.Format(CultureInfo.InvariantCulture, "{0} ({1} Output)",
				string.IsNullOrEmpty(File) ? "Unspecified" : File,
				TypeName);
		}

		public virtual void Convert(Input input, string baseDirectory) {
			throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, 
				"{0} input not supported for {1} output.", input.TypeDescription, TypeDescription));
		}

		protected virtual void WriteXml(XmlNode node, string baseDirectory) {

			XmlWriterSettings writerSettings = new XmlWriterSettings();
			writerSettings.NewLineHandling = NewLineHandling.Entitize;
			writerSettings.Indent = true;
			writerSettings.IndentChars = "\t";

			using(StreamWriter writer = new StreamWriter(Converter.ResolvePath(baseDirectory, File)))
			using(XmlWriter xmlWriter = XmlWriter.Create(writer, writerSettings))
				node.WriteTo(xmlWriter);

		}

	}

}